1. 题目描述(简单难度)

[success] 374. 猜数字大小

2. 解法一:二分法

public class Solution extends GuessGame {
    public int guessNumber(int n) {
        int low = 0;
        int high = n;
        while(low<=high){
            int middle = low + (high-low)/2;
            if(guess(middle) ==0){
                return middle;
            }
            else if(guess(middle) == 1){
                low = middle+1;
            }
            else{
                high = middle-1;
            }
        }
        return -1;
    }
}
© gaohueric all right reserved,powered by Gitbook文件修订时间: 2021-12-08 23:22:22

results matching ""

    No results matching ""